package com.zp.crm.Controller.LxrController;

import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.zp.crm.entity.LxrEntity.LxrEntity;
import com.zp.crm.service.LxrService.LxrService;

@Controller
@RequestMapping("/lxr")
public class LxrController {

	@Resource
	public LxrService lxrService;
	
	@RequestMapping(value="/select",method=RequestMethod.POST)
	public void selectCode(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");
		Map<String,Object> map=lxrService.selectCode(code);
		String a=(String)map.get("message");
		try{
			PrintWriter out =response.getWriter();
			out.println(a);
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public void insert(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String zw=request.getParameter("zw");
		String bgdh=request.getParameter("bgdh");
		String phone=request.getParameter("phone");
		String bz=request.getParameter("bz");
		LxrEntity entity=new LxrEntity(code, name, sex, zw, bgdh, phone, bz);
		Map<String,Object> map=lxrService.insert(entity);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String,Object> map=lxrService.delete(id);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}	
		
	}
	
}
